home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: A question on for loop
- Date: 14 Mar 1996 08:20:50 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4i9h12INN96u@keats.ugrad.cs.ubc.ca>
- References: <31471574.79D9@hdc-usa.com> <4i77ca$161@ccshst05.cs.uoguelph.ca>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4i77ca$161@ccshst05.cs.uoguelph.ca>,
- Toby K Hay <thay@uoguelph.ca> wrote:
- >Phil Boyd (boydp@hdc-usa.com) wrote:
- >: > |>
- >: > |> for(i=0;i< 20; i++)
- >: > |> {
- >: > ...
- >: > |> if(condition==FALSE)
- >: > |> continue;
- >: > ...
- >: > |> }
- >
- >: OK, now for a newbie question just for my own edification:
- >: Inside the for loop - does "i" start off with the value 0 so that
- >: the loop has the potential to repeat up to 20 times (assuming that
- >: "condition" remains FALSE)?
- >
- >My novice's answer is yes, i starts off at 0 because the for statement
- >(i=0;i<20;i++) has i++ meaning that i is incremented after the
- >operation. If it were (i=0;i<20;++i) then i would start at 1 and run up
- >to 20. Is this correct?
-
- Totally false. The i++ is the ``operation'', not the entire for() statement.
- It makes no difference at all whether you write i++ or ++i, since the value
- of the ``i++'' expression is not used for anything; only the side effect of
- the ++ is relied upon.
-
- Look at the grammar production of the for loop. It is something like this:
-
- for ( expression_opt ; expression_opt ; expression_opt ) statement
-
- Each expression is evaluated individually, including all of its side effects.
- --
-
-